package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.model.admin.dtos.AdSensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.admin.service.AdSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(AdSensitiveDto dto) {
        //核查页码是否正确，错误按照设置，查询
        dto.checkParam();

            Page page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();

        if(StringUtils.isNoneBlank(dto.getName())){

            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage result = page(page,wrapper);

        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal());

        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
    //检查参数
        if(adSensitive==null||StringUtil.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"参数不存在");
        }
        //判断敏感词是否存 在
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        int count=this.count(wrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }

        adSensitive.setCreatedTime(new Date());

        save(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //判断参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"id不存在");
        }
        //判断数据是否存在
        AdSensitive adSensitive = getById(id);
        if (adSensitive==null) {
            return  ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"数据不存在");
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {

            //检查参数
        if (adSensitive.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据不存在");
        }

        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        int count=count(wrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }

        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
